package jard.webshop.nbp;

import jard.webshop.util.Constants;
import java.io.Serializable;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 *
 * @author cjp
 */
@Entity
@NamedQueries({
    @NamedQuery(name = Constants.GET_ALL_VISIB_PROD, query = 
        "SELECT p FROM Product p WHERE p.visible = TRUE AND p.removed = FALSE"),
    @NamedQuery(name = Constants.GET_ALL_REMOVED_PROD, query = 
        "SELECT p FROM Product p WHERE p.removed = TRUE")
})
public class Product implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private String manufacturer;
    @JoinColumn
    @ManyToOne
    private Category category;
    @Column(nullable = false)
    private Float price;
    @Column(nullable = false)
    private Float discount = 0f;
    @Column(nullable = false)
    private Boolean onSale = false;
    @Column(nullable = false)
    private String salesTaxCat = "Standard";
    @Column(nullable = false)
    private Integer stock = 0;
    private Boolean removed = false;
    @Column(nullable = false)
    private String description = "No description available.";
    @Column(nullable = false)
    private Float weight = 0f;
    @ManyToMany(cascade = CascadeType.ALL)
    private List<Product> groupedWith = new LinkedList<Product>();
    @Column(nullable = false)
    private Boolean visible = true; // show only by direct uri if false, not in listings, true for one in groupedWith
    private String attr1 = "";
    private String attr2 = "";
    private String imgName; //file name of product image

    public Product() {
    }

    public Product(String name, String manufacturer, Category category, Float price) {
        this.name = name;
        this.manufacturer = manufacturer;
        this.category = category;
        this.price = price;
        this.description = "This is a great zombie t-shirt! Buy it!";
        this.imgName = "the_horde.jpg";
    }

    public Long getArticleNumber() {
        return id;
    }

    public void setArticleNumber(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Product)) {
            return false;
        }
        Product other = (Product) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getFullName();//"jard.webshop.nbp.Product[ id=" + id + ", " + name + ", "  + price + ", "  + category + ", " + stock + " ]";
    }

    public String getName() {
        return name;
    }

    public String getFullName() {
        if (attr1.length() == 0 && attr2.length() == 0) {
            return name;
        }
        String spacer = (attr1.length() == 0 || attr2.length() == 0 ? "" : ",");
        return name + " (" + attr1 + spacer + attr2 + ")  " + (price-discount) +
                " " + Constants.getCurrency();
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public Float getDiscount() {
        return discount;
    }

    public void setDiscount(Float discount) {
        this.discount = discount;
    }

    public Boolean getOnSale() {
        return onSale;
    }

    public void setOnSale(Boolean onSale) {
        this.onSale = onSale;
    }

    public String getSalesTaxCat() {
        return salesTaxCat;
    }

    public void setSalesTaxCat(String salesTaxCat) {
        this.salesTaxCat = salesTaxCat;
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    public Boolean getRemoved() {
        return removed;
    }

    public void setRemoved(Boolean removed) {
        this.removed = removed;
    }

    public List<Product> getGroupedWith() {
        return groupedWith;
    }

    public void setGroupedWith(List<Product> groupedWith) {
        this.groupedWith = groupedWith;
    }

    public void addGroupedWith(Product product) {
        this.groupedWith.add(product);
    }

    public Boolean getVisible() {
        return visible;
    }

    public void setVisible(Boolean visible) {
        this.visible = visible;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Float getWeight() {
        return weight;
    }

    public void setWeight(Float weight) {
        this.weight = weight;
    }

    public String getImgName() {
        return imgName;
    }

    public void setImgName(String imgName) {
        this.imgName = imgName;
    }

    public String getAttr1() {
        return attr1;
    }

    public void setAttr1(String attr1) {
        this.attr1 = attr1;
    }

    public String getAttr2() {
        return attr2;
    }

    public void setAttr2(String attr2) {
        this.attr2 = attr2;
    }
}
